package controllers.user;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ActorService;
import services.ProjectService;
import controllers.AbstractController;
import domain.Project;



@Controller
@RequestMapping("/project/user")
public class ProjectUserController extends AbstractController {

	//Services -----------------------------------------------------------------

	@Autowired
	ProjectService projectService;

	@Autowired
	ActorService actorService;


	// Constructors -----------------------------------------------------------

	public ProjectUserController() {
		super();
	}

	// List -------------------------------------------------------------------

	@RequestMapping("/listAvailable")
	public ModelAndView list() {
		ModelAndView result;
		Collection<Project> projects = projectService.findAvailableProjectsByGroupId();

		result = new ModelAndView("project/list");
		result.addObject("projects", projects);
		return result;
	}
	
	@RequestMapping("/listJoined")
	public ModelAndView listJoined(@RequestParam(required=false) String message) {
		ModelAndView result;
		Collection<Project> projects = projectService.findJoinedProjectsByUserId();

		result = new ModelAndView("project/list");
		result.addObject("projects", projects);
		result.addObject("message", message);
		return result;
	}
	
	@RequestMapping("/listCreated")
	public ModelAndView listCreated(@RequestParam(required=false) String message) {
		ModelAndView result;
		Collection<Project> projects = projectService.findProjectsByCreatorId();

		result = new ModelAndView("project/list");
		result.addObject("projects", projects);
		result.addObject("message", message);
		return result;
	}

	// Creation ---------------------------------------------------------------

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create() {
		ModelAndView result;
		Project project;
		project = projectService.create();
		result = createEditModelAndView(project);


		return result;
	}

	// Edition ----------------------------------------------------------------

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam int projectId) {
		ModelAndView result;
		Project project;

		project = projectService.findOne(projectId);		
		result = createEditModelAndView(project);

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Project project,BindingResult bindingResult) {
		ModelAndView result;		


		if (bindingResult.hasErrors()) {
			result = createEditModelAndView(project);

		} else {
			try {
				projectService.save(project);				
				result = new ModelAndView("redirect:/project/user/listJoined.do");
				result.addObject("message", "project.commit.success");
			} catch (Throwable oops) {
				result = createEditModelAndView(project, oops.getMessage());
			}
		}

		return result;
	}


	// Ancillary methods ------------------------------------------------------

	protected ModelAndView createEditModelAndView(Project project) {
		ModelAndView result;

		result = createEditModelAndView(project, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Project project, String message) {
		ModelAndView result;


		result = new ModelAndView("project/edit");
		result.addObject("project", project);
		result.addObject("message", message);


		return result;
	}

}